package com.hupu.jrs.widget

import android.app.Fragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import com.hupu.jrs.R
import com.hupu.jrs.widget.state.*

/**
 * Created by sll on 2015/3/13.
 */
open class ProgressFragment : Fragment() {

    var isPrepare = false

    private var mContentView: View? = null

    private var mEmptyState: ShowState? = null
    private var mProgressState: ShowState? = null
    private var mErrorState: ShowState? = null
    private var mContentState: ShowState? = null
    private val mLoginState: ShowState? = null
    private val mCollectState: ShowState? = null
    private var mAnimIn: Animation? = null
    private var mAnimOut: Animation? = null

    private var mLastState: ShowState = NonState()

    //Override this method to change content view
    open fun onCreateContentView(inflater: LayoutInflater): View? {
        return null
    }

    //Override this method to change error view
    open fun onCreateContentErrorView(inflater: LayoutInflater): View? {
        return null
    }

    open fun onCreateContentEmptyView(inflater: LayoutInflater): View? {
        return null
    }

    open fun onCreateProgressView(inflater: LayoutInflater): View? {
        return null
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle): View? {
        val main = inflater.inflate(R.layout.epf_layout, container, false) as ViewGroup

        val content = onCreateContentView(inflater)
        val error = onCreateContentErrorView(inflater)
        val empty = onCreateContentEmptyView(inflater)
        val progress = onCreateProgressView(inflater)

        replaceViewById(main, R.id.epf_content, content)
        replaceViewById(main, R.id.epf_error, error)
        replaceViewById(main, R.id.epf_empty, empty)
        replaceViewById(main, R.id.epf_progress, progress)

        mContentView = main

        mAnimIn = onCreateAnimationIn()
        mAnimOut = onCreateAnimationOut()

        initStates()
        isPrepare = true
        return main
    }

    fun onCreateAnimationIn(): Animation {
        return AnimationUtils.loadAnimation(activity, android.R.anim.fade_in)
    }

    fun onCreateAnimationOut(): Animation {
        return AnimationUtils.loadAnimation(activity, android.R.anim.fade_out)
    }

    private fun replaceViewById(container: ViewGroup, viewId: Int, newView: View?) {
        if (newView == null) {
            return
        }
        newView.id = viewId
        val oldView = container.findViewById<View>(viewId)
        val index = container.indexOfChild(oldView)
        container.removeView(oldView)
        container.addView(newView, index)

        newView.visibility = View.GONE
    }

    private fun initStates() {

        mEmptyState = EmptyState()
        mProgressState = ProgressState()
        mErrorState = ErrorState()
        mContentState = ContentState()

        initState(mEmptyState!!)
        initState(mProgressState!!)
        initState(mErrorState!!)
        initState(mContentState!!)
    }

    private fun initState(state: ShowState) {
        state.setAnimIn(mAnimIn!!)
        state.setAnimOut(mAnimOut!!)
        state.setFragmentView(mContentView!!)
    }

    fun showContent(animate: Boolean) {
        if (mLastState === mContentState) {
            return
        }
        mContentState!!.show(animate)
        mLastState.dismiss(animate)
        mLastState = mContentState!!
    }

    fun showEmpty(animate: Boolean) {
        if (mLastState === mEmptyState) {
            return
        }
        mEmptyState!!.show(animate)
        mLastState.dismiss(animate)
        mLastState = mEmptyState!!
    }

    fun showError(animate: Boolean) {
        if (mLastState === mErrorState) {
            return
        }
        mErrorState!!.show(animate)
        mLastState.dismiss(animate)
        mLastState = mErrorState!!
    }

    fun showProgress(animate: Boolean) {
        if (mLastState === mProgressState) {
            return
        }
        mProgressState!!.show(animate)
        mLastState.dismiss(animate)
        mLastState = mProgressState!!
    }
}
